<?php
namespace app\space\logic;
use app\space\validate\WorkStationMessage as WorkStationMessageValidate;

class SpaceLogic extends BaseLogic {

    /**
     * 获取工位查询条件
     * @param $branchid
     * @param $floorid
     * @param $use_status
     * @param $work_type
     * @param $keywords
     * @param $expire_status
     * @return array
     */
    public function get_search_ws_maps($branchid,$floorid,$use_status,$work_type,$keywords,$expire_status){
        $maps = [];
        if($branchid != -1){
            $f_maps = [];
            $f_maps[] = ['branchid','=',$branchid];
            $floorids = model('floor')->get_column($f_maps,'id');

            $m_maps = [];
            $m_maps[] = ['floorid','in',$floorids];
            $regionids = model('region')->get_column($m_maps,'id');
            $maps[] = ['regionid','in',$regionids];
        }

        if($floorid != -1){
            $m_maps = [];
            $m_maps[] = ['floorid','=',$floorid];
            $regionids = model('region')->get_column($m_maps,'id');
            $maps[] = ['regionid','in',$regionids];
        }

        if($use_status != -1){
            $now_date = date('Y-m-d');
            if($use_status == 0){
                $ws_maps = [];
                $ws_maps[] = ['company_out_time','<',$now_date];
                $wsids = model('work_station')->get_column($ws_maps,'id');
                $maps[] = ['id','in',$wsids];
            }else{
                $ws_maps = [];
                $wsids = model('work_station')->get_column($ws_maps,'id');
                $crwids = [];
                foreach($wsids as $k => $v){
                    $crw_maps = [];
                    $crw_maps[] = ['wsid','=',$v];
                    $crw_detail = model('cp_region_ws')->get_detail($crw_maps,'id','id desc');
                    $crwids[] = !empty($crw_detail) ? $crw_detail['id'] : 0;
                }
                if($use_status == 1){
                    $crw_maps = [];
                    $crw_maps[] = ['id','in',$crwids];
                    $crw_maps[] = ['use_status','=',$use_status];
                    $wsids = model('cp_region_ws')->get_column($crw_maps,'wsid');

                    $maps[] = ['id','in',$wsids];
                    $maps[] = ['company_out_time','>=',$now_date];
                }elseif($use_status == 2){
                    $crw_maps = [];
                    $crw_maps[] = ['id','in',$crwids];
                    $crw_maps[] = ['use_status','=',1];
                    $wsids = model('cp_region_ws')->get_column($crw_maps,'wsid');

                    $maps[] = ['id','not in',$wsids];
                    $maps[] = ['company_out_time','>=',$now_date];
                }
            }
        }

        if($work_type != -1){
            $maps[] = ['work_type','=',$work_type];
        }

        if(!empty($keywords)){
            $maps[] = ['number|company_name|company_person|company_tel','like','%' . $keywords . '%'];
        }

        if($expire_status != -1){
            $wsid = [];
            $now_date = date('Y-m-d');

            $ws_maps = [];
            $wsids = model('work_station')->get_list($ws_maps,'id,company_out_time');
            foreach($wsids as $k => $v){
                $diff_day = get_time_diff_day($v['company_out_time'],$now_date);
                if(($diff_day >= 0) && ($diff_day <= 30) && ($expire_status == 1)){
                    $wsid[] = $v['id'];
                }
                if($diff_day > 30 && ($expire_status == 2)){
                    $wsid[] = $v['id'];
                }
            }
            $maps[] = ['id','in',$wsid];
        }

        return $maps;
    }

    /**
     * 检查获取工位详情规则
     * @param $params
     * @return mixed
     */
    public function check_get_ws_detail_rule($params){
        $maps = [];
        $maps[] = ['id','=',$params['wsid']];

        $ws_detail = model('work_station')->get_detail($maps);
        if(empty($ws_detail)){
            return $this->get_response(0,'该工位不存在');
        }

        $data['ws_detail'] = $ws_detail;
        return $this->get_response(1,'OK',$data);
    }

    /**
     * 获取工位留言查询条件
     * @param $params
     * @return array
     */
    public function get_search_wsm_maps($params){
        $maps = [];

        $maps[] = ['wsid','=',$params['wsid']];
        $maps[] = ['enable','=',1];
        return $maps;
    }

    /**
     * 检查添加工位留言规则
     * @param $params
     * @return array
     */
    public function check_add_wsm_rule($params){
        $validate = new WorkStationMessageValidate();
        $r = $validate->scene('add')->check($params);
        if(!$r){
            return $this->get_response(0,$validate->getError());
        }

        $maps = [];
        $maps[] = ['id','=',$params['wsid']];
        $ws_detail = model('work_station')->get_detail($maps,'id,regionid,number,company_tel');
        if(empty($ws_detail)){
            return $this->get_response(0,'该工位不存在');
        }

        //查找区域
        $r_maps = [];
        $r_maps[] = ['id','=',$ws_detail['regionid']];
        $r_detail = model('region')->get_detail($r_maps,'id,floorid');

        //查找楼层
        $f_maps = [];
        $f_maps[] = ['id','=',$r_detail['floorid']];
        $f_detail = model('floor')->get_detail($f_maps,'id,branchid,name');
        $ws_detail['floor_zw'] = !empty($f_detail) ? $f_detail['name'] : '';

        //查找分部
        $b_maps = [];
        $b_maps[] = ['id','=',$f_detail['branchid']];
        $b_detail = model('branch')->get_detail($b_maps,'id,name');
        $ws_detail['branch_zw'] = !empty($b_detail) ? $b_detail['name'] : '';

        $data['ws_detail'] = $ws_detail;
        return $this->get_response(1,'OK',$data);
    }
}